﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using NAudio.Wave;

namespace MapleLeaf.Pay {

    public static class TradePlayer {

        private static Dictionary<string, byte[]> LoadAudios() {
            Assembly executingAssembly = Assembly.GetExecutingAssembly();
            IEnumerable<string> enumerable = from d in executingAssembly.GetManifestResourceNames()
                                             where d.EndsWith(".mp3")
                                             select d;
            Dictionary<string, byte[]> dictionary = new Dictionary<string, byte[]>();
            foreach (string text in enumerable) {
                using (Stream manifestResourceStream = executingAssembly.GetManifestResourceStream(text)) {
                    string[] array = text.Split(new char[]
                    {
                        '.'
                    });
                    string key = array[array.Length - 2];
                    dictionary[key] = manifestResourceStream.ReadBytes((int)manifestResourceStream.Length);
                }
            }
            return dictionary;
        }


        public static string ConvertToChinese(decimal number) {
            string input = number.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
            string input2 = Regex.Replace(input, "((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\\.]|$))))", "${b}${z}");
            string text = Regex.Replace(input2, ".", (Match m) => "负元点零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万亿兆京垓秭穰"[m.Value[0] - '-'].ToString());
            text = text.Replace("角", null);
            if (text.Contains('分')) {
                if (text.Length == 2) {
                    text = "零" + text;
                }
                text = text.Replace("分", null);
            }
            if (!text.Contains('元')) {
                text = "零点" + text + "元";
            }
            else if (!text.EndsWith("元", StringComparison.Ordinal)) {
                text = text.Replace("元", "点") + "元";
            }
            return text;
        }

      
        public static void Speak(string text) {
            List<string> list = new List<string>();
            int i = 0;

            while (i < text.Length) {
                for (int j = text.Length - i; j > 0; j--) {
                    string text2 = text.Substring(i, j);
                    if (Audios.ContainsKey(text2)) {
                        list.Add(text2);
                        i++;
                    }
                }

            }
            MemoryStream memoryStream = new MemoryStream();
            WaveFileWriter waveFileWriter = new WaveFileWriter(memoryStream, new WaveFormat());
            foreach (string key in list) {
                byte[] array = Audios[key];
                waveFileWriter.Write(array, 0, array.Length);
            }
            memoryStream.Position = 0L;
            Wave.Init(new Mp3FileReader(memoryStream));
            Wave.Play();
        }
        static TradePlayer() {
            Class18.LVfVa0hzXO4Jq();
            Audios = LoadAudios();
            Wave = new WaveOut();
        }

        private static Dictionary<string, byte[]> Audios;

        private static readonly WaveOut Wave;
    }
}
